package net.huashitong.Indicators.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.entity.Industry;
import net.huashitong.Indicators.filter.IndustryFilter;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class IndustryDao extends StringDao<Industry> {

    /**
     * 获取行业目录
     * @return
     */
    public List<Industry> getCatalog(){
        String hql = "select INDUSTRY_CODE as \"industryCode\",INDUSTRY_NAME as \"industryName\"," +
                "title as \"title\"，DATA_TIME as \"dataTime\",HAVE_DATA as \"haveData\",FREQUENCY as \"frequency\"," +
                "ORDERS as \"orders\" " +
                "from CA01 group by INDUSTRY_CODE,TITLE,INDUSTRY_NAME,DATA_TIME,HAVE_DATA,FREQUENCY,ORDERS " +
                "order by orders+0 asc,INDUSTRY_CODE asc,DATA_TIME desc";
//
//        SQLQuery query = getSession().createSQLQuery(hql).addScalar("industryCode")
//                .addScalar("industryName").addScalar("title").addScalar("dataTime");
        SQLQuery query = getSession().createSQLQuery(hql);
        query.setResultTransformer(Transformers.aliasToBean(Industry.class));
        List<Industry> dtos =( List<Industry>) query.list();
        return dtos;
    }
    /**
     * 行业动态查询
     * @param filter
     * @return
     */
    public List<Industry> queryIndustrys(IndustryFilter filter){
        GenericQuery query = createQuery("obj");
        //行业编码
        if(StringUtils.isNotBlank(filter.getIndustryCode())){
            query.append(" and obj.industryCode=:industryCode").setParam("industryCode", filter.getIndustryCode());
        }
        //时间
        if(StringUtils.isNotBlank(filter.getDataTime())){
            query.append(" and obj.dataTime=:dataTime").setParam("dataTime", filter.getDataTime());
        }
        //标题
        if(StringUtils.isNotBlank(filter.getTitle())){
            query.append(" and obj.title=:title").setParam("title", filter.getTitle());
        }
        //单位名称(模糊查询)
        if(StringUtils.isNotBlank(filter.getCompanyName())){
            query.append(" and obj.companyName like :companyName").setParam("companyName", filter.getCompanyName());
        }
        query.append(" order By obj.dataTime desc, obj.ranks+0 asc");
        return query.listResult();
    }

}
